define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'layer'], function ($, undefined, Backend, Table, Form, layer) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'promotion/setup/index' + location.search,
                    add_url: 'promotion/setup/add',
                    edit_url: 'promotion/setup/edit',
                    del_url: 'promotion/setup/del',
                    multi_url: 'promotion/setup/multi',
                    import_url: 'promotion/setup/import',
                    table: 'promotion_set',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'shop_id', title: __('Shop_id') },
                        { field: 'threshold', title: __('Threshold'), searchList: { "0": __('Threshold 0'), "1": __('Threshold 1'), "2": __('Threshold 2') }, formatter: Table.api.formatter.normal },
                        { field: 'good_ids', title: __('Good_ids'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        { field: 'atype', title: __('Atype'), searchList: { "0": __('Atype 0'), "1": __('Atype 1') }, formatter: Table.api.formatter.normal },
                        { field: 'oktype', title: __('Oktype'), searchList: { "0": __('Oktype 0'), "1": __('Oktype 1') }, formatter: Table.api.formatter.normal },
                        { field: 'customer', title: __('Customer'), searchList: { "0": __('Customer 0'), "1": __('Customer 1'), "2": __('Customer 2') }, formatter: Table.api.formatter.normal },
                        { field: 'purchase', title: __('Purchase'), searchList: { "0": __('Purchase 0'), "1": __('Purchase 1') }, formatter: Table.api.formatter.normal },
                        { field: 'stype', title: __('Stype'), searchList: { "0": __('Stype 0'), "1": __('Stype 1') }, formatter: Table.api.formatter.normal },
                        { field: 'balance', title: __('Balance'), searchList: { "0": __('Balance 0'), "1": __('Balance 1'), "2": __('Balance 2') }, formatter: Table.api.formatter.normal },
                        { field: 'ctype', title: __('Ctype'), searchList: { "0": __('Ctype 0'), "1": __('Ctype 1') }, formatter: Table.api.formatter.normal },
                        { field: 'dtype', title: __('Dtype'), searchList: { "0": __('Dtype 0'), "1": __('Dtype 1') }, formatter: Table.api.formatter.normal },
                        { field: 'itype', title: __('Itype'), searchList: { "0": __('Itype 0'), "1": __('Itype 1'), "2": __('Itype 2'), "3": __('Itype 3') }, formatter: Table.api.formatter.normal },
                        { field: 'pname', title: __('Pname'), operate: 'LIKE' },
                        { field: 'pdescribe', title: __('Pdescribe') },
                        { field: 'status', title: __('Status'), searchList: { "0": __('Status 0'), "1": __('Status 1') }, formatter: Table.api.formatter.status },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'shop.id', title: __('Shop.id') },
                        { field: 'shop.user_id', title: __('Shop.user_id') },
                        { field: 'shop.shopname', title: __('Shop.shopname'), operate: 'LIKE' },
                        { field: 'shop.keywords', title: __('Shop.keywords'), operate: 'LIKE' },
                        { field: 'shop.description', title: __('Shop.description'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        { field: 'shop.service_ids', title: __('Shop.service_ids'), operate: 'LIKE' },
                        { field: 'shop.avatar', title: __('Shop.avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'shop.state', title: __('Shop.state') },
                        { field: 'shop.level', title: __('Shop.level') },
                        { field: 'shop.islive', title: __('Shop.islive') },
                        { field: 'shop.isself', title: __('Shop.isself') },
                        { field: 'shop.city', title: __('Shop.city'), operate: 'LIKE' },
                        { field: 'shop.return', title: __('Shop.return'), operate: 'LIKE' },
                        { field: 'shop.like', title: __('Shop.like') },
                        { field: 'shop.score_describe', title: __('Shop.score_describe'), operate: 'BETWEEN' },
                        { field: 'shop.score_service', title: __('Shop.score_service'), operate: 'BETWEEN' },
                        { field: 'shop.score_deliver', title: __('Shop.score_deliver'), operate: 'BETWEEN' },
                        { field: 'shop.score_logistics', title: __('Shop.score_logistics'), operate: 'BETWEEN' },
                        { field: 'shop.weigh', title: __('Shop.weigh') },
                        { field: 'shop.verify', title: __('Shop.verify') },
                        { field: 'shop.createtime', title: __('Shop.createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'shop.updatetime', title: __('Shop.updatetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'shop.deletetime', title: __('Shop.deletetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'shop.status', title: __('Shop.status'), formatter: Table.api.formatter.status },
                        { field: 'shop.code', title: __('Shop.code'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        { field: 'shop.type', title: __('Shop.type') },
                        { field: 'shop.auth_id', title: __('Shop.auth_id') },
                        { field: 'shop.person', title: __('Shop.person'), operate: 'LIKE' },
                        { field: 'shop.telephone', title: __('Shop.telephone'), operate: 'LIKE' },
                        { field: 'shop.staff_id', title: __('Shop.staff_id') },
                        { field: 'shop.maturitytime', title: __('Shop.maturitytime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'shop.payment', title: __('Shop.payment'), operate: 'BETWEEN' },
                        { field: 'shop.settled', title: __('Shop.settled'), operate: 'BETWEEN' },
                        { field: 'shop.adds', title: __('Shop.adds'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        { field: 'shop.rightshop', title: __('Shop.rightshop') },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'promotion/setup/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'promotion/setup/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'promotion/setup/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {            
            if ($('.radio-ipt:checked').val() != 2) {
                $('.select-good').hide()
            }else{
                $('.select-good').show()
            }
            $('.radio-ipt').on('change', function () {
                if ($(this).val() == 2) {
                    $('.select-good').show()
                } else {
                    $('.select-good').hide()
                }
            })

            $('.image-box .item').eq($('#c-itype').val()).addClass('active')
            $('.image-box .item').on('click', function () {
                // 移除所有选项卡和内容的 active 类
                $('.image-box .item').removeClass('active');
                $('#c-itype').val($(this).index())

                // 为当前选项卡和对应的内容添加 active 类
                $(this).addClass('active');
            });

            $('.image-box span').on('click', function () {
                var imageUrl = '/assets/img/sl1.jpg';
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 1,
                    area: ['80%', '80%'],
                    content: '<div class="image-popup"><img src="' + imageUrl + '" alt="Image"></div>'
                });
            });
            $('.wenan').on('click', function () {
                var imageUrl = '/assets/img/sl2.jpg';
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 1,
                    area: ['80%', '80%'],
                    content: '<div class="image-popup"><img src="' + imageUrl + '" alt="Image"></div>'
                });
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
